Spring Boot |
您所在的位置:网站首页 › idea启动spring boot › Spring Boot |
简介
本文写给第一次使用IDEA搭建springboot项目的萌新们。 网上相关的教程也很多了,但自己在实际跟着操作的过程中还是碰到了一些问题,因此记录下来,希望能帮助到大家~ 最后的Hello, world界面如下: 前面基础的步骤都可以参考这篇博文(如果遇到什么问题再来看我的吧,可能可以帮助到你): 使用idea快速搭建一个springboot项目(遇到的小坑) 下面上自己傻瓜式详细步骤。 1.创建项目打开我们的IDEA,新建项目:
我很纳闷的是只有我新建好项目之后,这里没有自动给我配置好的吗(还是做教程的大佬们觉得没必要说qwq,直接就Run了)
打开文件目录,可以看到这里是不太吉祥的红色:
在这个DemoApplication上右键Run一下:
但是底下有报错:
右键pom.xml->Maven->Download Sources
那么怀着激动的心情,我们再Run一下: 好像很正常的亚子?
其实这里的重点是DataSource!! 直接上这篇博客,写的非常清楚Resolving “Failed to Configure a DataSource” Error By design, Spring Boot auto-configuration tries to configure the beans automatically based on the dependencies added to the classpath. And, since we have the JPA dependency on our classpath, Spring Boot tries to automatically configure a JPA DataSource. The problem is, we haven’t given Spring the information it needs to perform the auto-configuration. For example, we haven’t defined any JDBC connection properties, and we’ll need to do so when working with external databases like MySQL and MSSQL. On the other hand, we won’t face this issue with in-memory databases like H2 since they can create a data source without all this information. 记得之前创建项目的时候我们选了个MySQL Driver吗!Spring Boot喜欢自动把我们需要的环境都配置好,但是我们现在没有给它配置数据库的信息,所以出现了这个报错。 解决方案如下: 方法一:配置数据库信息 在 application.properties 中配置: spring.datasource.url=jdbc:mysql://localhost:3306/myDb spring.datasource.username=user1 spring.datasource.password=pass spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver或者在 application.yml 中配置: spring: datasource: driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/myDb username: user1 password: pass方法二:取消自动配置数据库的功能 在 application.properties 中使用 spring.autoconfigure.exclude 属性取消 auto-configuration。 spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration我就是用的这个方法(我是想着数据库配置啥的之后再说~)
以上方法任意选一种就可以。前面的博文里还有别的方法,我就没有列出来啦~ 这样弄好了之后我们再Run一下看看:
建立一个controller的文件夹:
那么最后再来Run一下:
访问网址 http://localhost:8080/demo/index,看到下面的页面就说明你成功啦~~) 一起秃头吧qwq |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |